package com.jumper.study.admin.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import com.jumper.study.admin.service.BannerService;
import com.jumper.study.db.model.Banner;
import com.jumper.study.admin.dto.req.BannerReq;
import com.jumper.study.admin.dto.resp.BannerResp;
import com.jumper.study.admin.dto.req.ModelStatusReq;
import com.jumper.study.common.api.CommonResult;


@RestController
@RequestMapping("/api/backend/banners")
public class BannerController {
    @Autowired
    private BannerService bannerService;

    @GetMapping("")
    public CommonResult<List<BannerResp>> getList() {
        List<BannerResp> resp = bannerService.list().stream()
            .map(entity -> new BannerResp(entity.getId(), entity.getTitle(), entity.getCover(), entity.getLink(), entity.getOnself(), entity.getUpdatedAt()))
            .toList();

        return CommonResult.success(resp);
    }

    @GetMapping("/{id}")
    public CommonResult<BannerResp> getItem(@PathVariable Long id) {
        Banner banner = bannerService.getById(id);
        if (banner == null) {
            return CommonResult.notFound();
        }

        return CommonResult.success(new BannerResp(banner.getId(), banner.getTitle(), banner.getCover(), banner.getLink(), banner.getOnself(), banner.getUpdatedAt()));
    }

    @PostMapping("/")
    public CommonResult<Boolean> create(@RequestBody @Validated BannerReq req) {
        Banner entity = new Banner();
        entity.setTitle(req.getTitle());
        entity.setCover(req.getCover());
        entity.setLink(req.getLink());
        entity.setOnself(req.getOnself());

        boolean result = bannerService.save(entity);
        return CommonResult.success(result);
    }

    @PostMapping("/{id}")
    public CommonResult<Boolean> update(@PathVariable Long id, @RequestBody @Validated BannerReq req) {
        Banner banner = bannerService.getById(id);
        if (banner == null) {
            return CommonResult.notFound();
        }
        banner.setTitle(req.getTitle());
        banner.setCover(req.getCover());
        banner.setLink(req.getLink());
        banner.setOnself(req.getOnself());

        boolean result = bannerService.updateById(banner);
        return CommonResult.success(result);
    }

    @DeleteMapping("/{id}")
    public CommonResult<Boolean> delete(@PathVariable Long id) {
        Banner banner = bannerService.getById(id);
        if (banner == null) {
            return CommonResult.success(true);
        }

        boolean result = bannerService.removeById(id);
        return CommonResult.success(result);
    }

    @PostMapping("/onself")
    public CommonResult<Boolean> onself(@RequestBody ModelStatusReq req) {
        boolean result = bannerService.onselfBatchById(req.getIds(), req.status());

        return CommonResult.success(result);
    }
}
